package com.dataseek.iot.analysis.manager.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.dataseek.iot.analysis.manager.service.DataService;
import com.dataseek.iot.core.constant.ResponseConst;
import com.dataseek.iot.core.jwt.JwtTokenProvider;
import com.dataseek.iot.core.response.ResponseEntity;
import com.dataseek.iot.core.util.AuthUtil;
import com.dataseek.iot.mysql.entity.*;
import com.dataseek.iot.mysql.service.impl.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author cc
 * @Title: 数据分析接口
 * @Description: 数据分析接口
 * @date 2021/10/258:26 上午
 * @since 1.0
 */
@RestController
@RequestMapping("/analysis")
@Api(tags = "数据分析接口")
public class AnalysisController {

    @Autowired
    JwtTokenProvider jwtTokenProvider;

    @Autowired
    UserServiceImpl userService;

    @Autowired
    LineServiceImpl lineService;

    @Autowired
    DeviceServiceImpl deviceService;

    @Autowired
    AlarmLogServiceImpl alarmLogService;

    @Autowired
    DeviceRecordServiceImpl deviceRecordService;

    @Autowired
    DataService dataService;


    /**
     * 获取过去7天内的数据趋势
     * @param exchange
     * @return
     */
    @GetMapping("/data/trend")
    @ApiOperation("获取过去七天内的数据")
    public Mono<ResponseEntity> sevenDayData(ServerWebExchange exchange){
        String username = AuthUtil.getUsername(exchange,jwtTokenProvider);
        List<Device> devices = dataService.getAllDevices(username);
        ResponseEntity entity = new ResponseEntity();
        Map<String,List> map = new HashMap<>();
        try{
            for(Device d : devices){
                String code =  d.getDeviceCode();
                List<DeviceRecord> list = deviceRecordService.getRecordsInPast(code, 7);
                map.put(code, list);
            }
            entity.setCode(ResponseConst.SUCCESS_CODE);
            entity.setMsg(ResponseConst.SUCCESS);
            entity.setData(map);
        }catch (Exception e){
            entity.setCode(ResponseConst.CRUD_FAILED_CODE);
            entity.setMsg(ResponseConst.CRUD_FAILED);
            entity.setData("");
            e.printStackTrace();
        }
        return Mono.just(entity);
    }

    @GetMapping("/data/trend/{code}/{num}")
    @ApiOperation("数据历史曲线")
    public Mono<ResponseEntity> dataHistory(@PathVariable String code, @PathVariable Integer num){
        ResponseEntity entity = new ResponseEntity();
        try{
            List<DeviceRecord> list =deviceRecordService.getRecordsInPast(code,num);

            entity.setCode(ResponseConst.SUCCESS_CODE);
            entity.setMsg(ResponseConst.SUCCESS);
            entity.setData(list);
        }catch (Exception e){
            entity.setCode(ResponseConst.CRUD_FAILED_CODE);
            entity.setMsg(ResponseConst.CRUD_FAILED);
            entity.setData("");
            e.printStackTrace();
        }
        return Mono.just(entity);
    }


    @GetMapping("/log/trend")
    @ApiOperation("日志7日历史曲线")
    public Mono<ResponseEntity> sevenDayAlarm(ServerWebExchange exchange){
        String username = AuthUtil.getUsername(exchange,jwtTokenProvider);
        List<Device> devices = dataService.getAllDevices(username);
        ResponseEntity entity = new ResponseEntity();
        Map<String,List> map = new HashMap<>();
        try{
            for(Device d : devices){
                String code =  d.getDeviceCode();
                List<DeviceLog> list = alarmLogService.getLogInPast(code, 7);
                map.put(code, list);
            }
            entity.setCode(ResponseConst.SUCCESS_CODE);
            entity.setMsg(ResponseConst.SUCCESS);
            entity.setData(map);
        }catch (Exception e){
            entity.setCode(ResponseConst.CRUD_FAILED_CODE);
            entity.setMsg(ResponseConst.CRUD_FAILED);
            entity.setData("");
            e.printStackTrace();
        }
        return Mono.just(entity);
    }

    @GetMapping("/log/trend/{code}/{num}")
    @ApiOperation("日志历史曲线")
    public Mono<ResponseEntity> logHistory(@PathVariable String code, @PathVariable Integer num){
        ResponseEntity entity = new ResponseEntity();
        try{
            List<DeviceLog> list =alarmLogService.getLogInPast(code,num);
            entity.setCode(ResponseConst.SUCCESS_CODE);
            entity.setMsg(ResponseConst.SUCCESS);
            entity.setData(list);
        }catch (Exception e){
            entity.setCode(ResponseConst.CRUD_FAILED_CODE);
            entity.setMsg(ResponseConst.CRUD_FAILED);
            entity.setData("");
            e.printStackTrace();
        }
        return Mono.just(entity);
    }


    /**
     * 获取用户下所有设备信息
     * @param exchange
     * @return
     */
    @GetMapping("/totalDevice")
    public Mono<ResponseEntity> totalDevice(ServerWebExchange exchange){
        String username = AuthUtil.getUsername(exchange,jwtTokenProvider);
        ResponseEntity entity = new ResponseEntity();
        entity.setMsg(ResponseConst.SUCCESS);
        entity.setCode(ResponseConst.SUCCESS_CODE);
        entity.setData(dataService.getAllDevices(username));
        return Mono.just(entity);
    }




}
